var price = '';
var postage = '';
var buynum = '';
var _id;
apiready = function() {

      _id = api.pageParam.id;
      var buyType = api.pageParam.type;
      $api.do_get("/api/products/info?id=" + _id, function(result) {
            var productPrice = 0;
            $("#buyNum").text(1);
            //如果type为空的话，则为拼团
            if (buyType) {
                  productPrice = result.price;
            } else {
                  //显示拼团价格
                  productPrice = result.gPrice;
            }
            $("#gprice").text(productPrice);
            $("#prototal").text(productPrice);
            $("#postage").text(result.postage);
            $("#proinfoName").text(result.name);
            $("#total").text(productPrice + result.postage);
            $api.getNetPicPath(result.sgImg, function(picPath) {
                  $("#info_img").attr('src', picPath);
            });

            $("#proinfoName").text(result.name);
            price = $('#gprice').text();
            postage = $('#postage').text();
            buynum = $("#buyNum").val();
      });


      $('.spinnerExample').spinner({
            value: 1,
            min: 1,
            max: 100,
            step: 1
      });

};

$("#orderinfo_product").click(function() {
      $api.mOpenWin("groupInfo", "group_info.html", {
            id: _id
      });
});

$("#city_2").citySelect({
      prov: "广东",
      nodata: "none"
});

$("#order_minus").click(function() {
      var buynum = $("#buyNum").val();
      if (buynum > 1) {
            $("#buyNum").val(buynum - 1);
            $("#prototal").text((buynum - 1) * price);
            $("#total").text((buynum - 1) * price + parseInt(postage));

      } else {

      }

});

$("#order_add").click(function() {
      var buynum = $("#buyNum").val();
      $("#buyNum").val(buynum + 1);
      $("#prototal").text((buynum + 1) * price);
      $("#total").text((buynum + 1) * price + parseInt(postage));
});

$("#buyNum").change(function() {
      var price = $('#gprice').text();
      var postage = $('#postage').text();
      var buynum = this.value;
      $("#prototal").text(buynum * price);
      $("#total").text(buynum * price + parseInt(postage));

});

$('#btnOrder').click(function() {

      var account;
      $api.getUserInf(function(user) {
            account = user.account;
            var data = {
                  id: api.pageParam.id,
                  account: account,
                  buyNum: $('#buyNum').val(),
                  address: $('.prov').val() + '-' + $('.city').val() + '-' + $('.dist').val() + '  ' + $('#address').val(),
                  receiver: $('#receiver').val(),
                  phone: $('#phone').val(),
                  orderType: '拼团',
            };


            if (!data.address || !data.receiver || !data.phone) {
                  $api.toastMsg('请填写收货信息');
                  return;
            }

            var postUrl;
            //如果type的值为1，证明是个人购买
            if (api.pageParam.type === 1) {
                  //创建单人购买
                  postUrl = '/api/product/order/create';
                  delete data.orderType;
            } else {
                  //创建团购订单地址
                  postUrl = '/api/pgroup/order/create';
            }



            api.showProgress();
            $api.do_post(postUrl, {
                  body: JSON.stringify(data)
            }, function(result) {
                  if (result.code == '0') {
                        $api.mOpenWin('orderPay', '../orderpay.html', {
                              payAmount: result.total,
                              orderNo: result.orderno
                        });
                        // window.location.href = '/wx/pgroup/orderpay?orderno=' + response.orderno;
                  } else {
                        $api.log(result);
                  }
            });


      });
});